package com.internetCafes.spms.web.tenant.service.impl;

import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.generic.GenericDao;
import com.internetCafes.spms.core.generic.GenericServiceImpl;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.internetCafes.spms.web.tenant.dao.LandingTimeInfoMapper;
import com.internetCafes.spms.web.tenant.model.LandingTimeInfo;
import com.internetCafes.spms.web.tenant.service.LandingTimeInfoService;


/**
 * 登陆时长记录表 LandingTimeInfoService 实现类
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2019-12-11 17:28:39
 */
@Service("landingTimeInfoService")
public class LandingTimeInfoServiceImpl extends GenericServiceImpl<LandingTimeInfo, Long> implements LandingTimeInfoService {

    @Autowired
    private LandingTimeInfoMapper landingTimeInfoMapper;

    @Value("${spring.pages.expire}")
    private int expire;

    @Override
    public List<LandingTimeInfo> query(Map<String, Object> params) {
        return landingTimeInfoMapper.selectByParams(params);
    }

    @Override
    public LandingTimeInfo findById(Long id) {
        return landingTimeInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<LandingTimeInfo> queryByIds(List<Long> ids) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);
        return landingTimeInfoMapper.selectByParams(params);
    }

    @Override
    public List<LandingTimeInfo> page(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return landingTimeInfoMapper.selectByParams(params);
    }

    @Override
    public long count(Map<String, Object> params) {
        return landingTimeInfoMapper.countByParams(params);
    }

    @Override
    public LandingTimeInfo findFirst(Map<String, Object> params) {
        List<LandingTimeInfo> list = landingTimeInfoMapper.selectByParams(params);
        LandingTimeInfo landingTimeInfo =null;
        if (list != null && list.size() > 0) {
            landingTimeInfo =list.get(0);
        }
        return landingTimeInfo;
    }

    @Override
    public LandingTimeInfo findLastest(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("page", 0);
        params.put("rows", 1);
        List<LandingTimeInfo> list = landingTimeInfoMapper.selectByParams(params);
        LandingTimeInfo landingTimeInfo =null;
        if (list != null && list.size() > 0) {
            landingTimeInfo =list.get(0);
        }
        return landingTimeInfo;
    }

    @Override
    public void saveOrUpdateLoginLog(Long userId, Date nowDate) {
        LandingTimeInfo l = findLastest(userId);
        if(l == null || l.getUpdateTime().getTime() + expire * 1000 < nowDate.getTime()){
            save(new LandingTimeInfo(userId, nowDate, nowDate));
        } else {
            l.setExpire(l.getExpire() + (nowDate.getTime() - l.getUpdateTime().getTime())/1000);
            l.setUpdateTime(nowDate);
            update(l);
        }
    }

    @Override
    public int save(LandingTimeInfo landingTimeInfo) {
        return landingTimeInfoMapper.insert(landingTimeInfo);
    }

    @Override
    public int saveBatch(List<LandingTimeInfo> landingTimeInfoList) {
        return landingTimeInfoMapper.insertBatch(landingTimeInfoList);
    }

    @Override
    public int saveSelective(LandingTimeInfo landingTimeInfo) {
        return landingTimeInfoMapper.insertSelective(landingTimeInfo);
    }

    @Override
    public int update(LandingTimeInfo landingTimeInfo) {
        return landingTimeInfoMapper.updateByPrimaryKey(landingTimeInfo);
    }

    @Override
    public int updateSelective(LandingTimeInfo landingTimeInfo) {
        return landingTimeInfoMapper.updateByPrimaryKeySelective(landingTimeInfo);
    }

    @Override
    public int delete(Long id) {
        return landingTimeInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return landingTimeInfoMapper.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        if(ids != null && ids.length > 0){
            for(Long id : ids){
                delete(id);
            }
        }
    }

    @Override
    public GenericDao<LandingTimeInfo, Long> getDao() {
        return landingTimeInfoMapper;
    }

    /**
     * DataGrid
     */
    @Override
    public DataGrid dataGrid(Map<String, Object> params, int pageNo, int pageSize) {

        List<LandingTimeInfo> landingTimeInfoList = page(params, (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize, pageSize);
        long count = count(params);

        DataGrid dg = new DataGrid(landingTimeInfoList, count, pageSize, pageNo);
        return dg;
    }
}